package com.think.hadoopTools;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.IOException;
import java.io.InputStream;

public class Decompress {
    public static void main(String[] args) {
        String sourceFile = null;
        String destFile = null;
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-s")) {//要解压的文件
                sourceFile = args[++i];
            } else if (args[i].equals("-d")) {//解压后的位置
                destFile = args[++i];
            }
        }
        System.out.println("sourceFile:" + sourceFile);
        System.out.println("destFile:" + destFile);
        decompress(sourceFile, destFile);
    }

    public static void decompress(String sourceFile, String destFile) {
        InputStream in = null;
        FSDataOutputStream outputStream = null;
        try {
            Class<?> codecClass = Class.forName(GzipCodec.class.getName());
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
            FSDataInputStream inputStream = fs.open(new Path(sourceFile));
            in = codec.createInputStream(inputStream);
            outputStream = fs.create(new Path(destFile));//输出到hdfs
            //FileOutputStream fos = new FileOutputStream(new File(filename + ".decode"));//输出到本机文件系统
            //1024 * 1024 * 4=4M会出现DFSOutputStream ResponseProcessor exception  for block blk_-6539369062504473952_7483087java.io.EOFException
            //在 hdfs-site.xml 增大dfs.datanode.socket.write.timeout和dfs.socket.timeout两个配置项即可
            //IOUtils.copyBytes(in, outputStream, 1024 * 1024 * 4);
            IOUtils.copyBytes(in, outputStream, conf);//默认4096
            //IOUtils.copyBytes(in, System.out, conf);//输出到控制台
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(in);
            IOUtils.closeStream(outputStream);
        }
    }
}
